<script setup>
import ActivityNoteList from "@/views/contract/common/activityNote/activityNoteList.vue";
import {
  applyAudit,
  auditComplete,
  isNeedWordToPDF
} from "@/api/contract/myContract.js";
import {pageofficeOpenFile} from "@/plugins/pageoffice/pageoffice-common.js";
import SelectUserList from "@/views/common/selectUserList.vue";
import {backMyConList} from "@/utils/backList.js";
import {isNull} from "@/utils/validate.js";
import {filemakerctrl} from "js-pageoffice";
import modal from "@/plugins/modal.js";
import CancelButton from "@/views/contract/my/flowInfo/common/cancelButton.vue";
import {flowInfoFunctions, setCurrentIsRefreshActivityNote} from "@/utils/contract-my.js";
import {refreshParentRevisionCountFunction} from "@/plugins/pageoffice/pageoffice-word-excel.js";

const {proxy} = getCurrentInstance()
const selectUserListRef = ref(null)
const loading = ref(false)
const prop = defineProps({
  conId: {
    type: Number
  },
  revisionCount: {
    type: Number
  },
  flowInfoButtons: {
    type: JSON
  },
  isRefreshActivityNote: {
    type: Boolean,
  }
})
const revisionCount = ref(prop.revisionCount)
const { refreshRevisionCount } = refreshParentRevisionCountFunction(revisionCount)
const currentIsRefreshActivityNote = ref(prop.isRefreshActivityNote)
const conId = prop.conId
const {
  refreshMainViewData,
  handleApply,
  getApplyUserList,
  handleSelectedUser
} = flowInfoFunctions(prop, conId, applyAudit, loading, selectUserListRef, currentIsRefreshActivityNote)

function openFile() {
  pageofficeOpenFile('/contract/my/audit/openFile/' + conId, conId)
}

function handleNext() {
  if (isNull(revisionCount.value) || revisionCount.value > 0) {
    proxy.$modal.alertWarning('文件正文中尚有 ' + revisionCount.value + ' 条修订痕迹尚未处理，请先处理完所有修订痕迹后再执行当前操作')
    return false
  }
  isNeedWordToPDF(conId).then((isNeedWordToPDF) => {
    if (isNeedWordToPDF) {
      modal.confirm('确认已审核完毕，进入签订中？').then(() => {
        modal.loading('正在执行，请稍后...')
        return wordToPDF(conId)
      })
    } else {
      proxy.$modal.confirmAndExecuteJS('确认已审核完毕，进入签订中？', auditComplete, conId, refreshMainViewData)
    }
  })
}

function wordToPDF(conId) {
  filemakerctrl.SaveFilePage = '/con/my/auditCompleteWithSavePdf/' + conId
  filemakerctrl.CallFileMaker({
    url: "/con/my/wordToPdf/" + conId,
    success: function () {
      modal.closeLoading()
      refreshMainViewData()
    },
    error: function (msg) {
      modal.closeLoading()
      proxy.$modal.alertError(msg)
    }
  })
}

</script>

<template>
  <el-row v-loading="loading">
    <el-col :span="12" class="left">
      <el-row>
        <el-form-item>
          <el-button @click="backMyConList">返 回</el-button>
          <el-button type="primary" @click="openFile">打开正文</el-button>
          <el-button type="primary" @click="handleApply" v-if="flowInfoButtons.isApply">申请审核</el-button>
          <el-button type="success" @click="handleNext" v-if="flowInfoButtons.isNext">审核完毕</el-button>
        </el-form-item>
      </el-row>
    </el-col>
    <el-col :span="12">
      <el-row justify="end">
        <el-form-item>
          <CancelButton :conId="conId" :isCancel="flowInfoButtons.isCancel"/>
        </el-form-item>
      </el-row>
    </el-col>
  </el-row>

  <!-- 选择用户对话框 -->
  <SelectUserList ref="selectUserListRef" @getApplyUserList="getApplyUserList" @handleSelectedUser="handleSelectedUser"/>

  <!-- 流程信息列表 -->
  <ActivityNoteList :isRefreshActivityNote="currentIsRefreshActivityNote" :isCanAddActivityNote="true"/>
</template>

